package com.bestcem.xm.ticket.dao.impl.mysql;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.ticket.dao.NotificationDao;
import com.bestcem.xm.ticket.dao.converter.NotificationConverter;
import com.bestcem.xm.ticket.dao.mapper.NotificationDaoMapper;
import com.bestcem.xm.ticket.entity.mongo.Notification;
import com.bestcem.xm.ticket.entity.mysql.NotificationDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 问卷项目的通知方式记录表 DAO
 *
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version 通知提醒新增微信公众号需求
 * @date 2021/9/17 15:58
 */
@Repository
@Slf4j
public class NotificationDaoImpl implements NotificationDao {

    @Resource
    private NotificationDaoMapper notificationDaoMapper;

    @Override
    public Notification findBySurveyId(String surveyId) {
        NotificationDO notificationDO = notificationDaoMapper.findBySurveyId(surveyId);

        return NotificationConverter.doToEntity(notificationDO);
    }

    @Override
    public List<Notification> findBySurveyNotNull() {
        List<NotificationDO> list = notificationDaoMapper.findBySurveyNotNull();

        return NotificationConverter.doListToDtoList(list);
    }

    /**
     * 保存通知
     *
     * @param entity
     * @return Notification
     * @author jy.zhao
     * @date 2021/8/23 15:17
     * @date 2022/4/7 12:16 XiaoYunSong 通知提醒新增微信公众号需求 新增 是否开启免打扰/是否开启微信公众号通知
     */
    @Override
    public Notification saveNotification(Notification entity) {
        NotificationDO isDo = NotificationConverter.entityToDo(entity);
        if (Objects.isNull(isDo)) {
            return null;
        }

        // 通知提醒新增微信公众号需求 新增 免打扰开发 字段
        if (Objects.isNull(isDo.getNoDisturbEnable())) {
            isDo.setNoDisturbEnable(!CollectionUtils.isEmpty(entity.getNoDisturb()));
        }

        if (Objects.isNull(isDo.getNotificationId())) {
            isDo.setNotificationId(ObjectId.next());
        }
        if (Objects.isNull(isDo.getCreateTime())) {
            isDo.setCreateTime(DateUtil.getCommonDate());
        }
        if (Objects.isNull(isDo.getUpdateTime())) {
            isDo.setUpdateTime(DateUtil.getCommonDate());
        }

        notificationDaoMapper.insert(isDo);
        isDo = notificationDaoMapper.findByNotificationId(isDo.getNotificationId());

        return NotificationConverter.doToEntity(isDo);
    }


    /**
     * 根据问卷id更新通知
     *
     * @author gy.liu
     * @date 2021/8/25
     * @date 2022/4/7 12:16 XiaoYunSong 通知提醒新增微信公众号需求 新增 是否开启免打扰/是否开启微信公众号通知
     */
    @Override
    public long updateNotificationBySurveyId(Notification entity) {
        NotificationDO entityDo = NotificationConverter.entityToDo(entity);
        if (Objects.isNull(entityDo)) {
            return 0L;
        }

        entityDo.setUpdateTime(DateUtil.getCommonDate());
        if (log.isInfoEnabled()) {
            log.info("[Ticket] 复制提醒last：param={}", entityDo.getSample());
        }
        return notificationDaoMapper.updateBySurveyId(entityDo);
    }
}
